﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sale_Operation;
using Sale_Common;
using Sale_Model;
using SaleSystem;
using ExtAspNet;

namespace SaleSystem.BaseData
{
    public partial class ProductSortEdit : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                InitControl();
             
                GetAndBind();
            }
        }
        protected void bnSave_Click(object sender, EventArgs e)
        {
            SModel.PSort_Code = this.tb_PSort_Code.Text;
            SModel.PSort_Name = this.tb_PSort_Name.Text;            
            SModel.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SModel.CreateTime = System.DateTime.Now;
            SModel.PSort_ID = Id;
            SModel.PSort_PSort_ID = ValueHandler.GetIntNumberValue(Request["PSort_PSort_ID"]);
            SModel.PSort_Type = 0; 
            SModel.State = 1; 
            bool blHasExists = Manager.HasExists(SModel);
            if (blHasExists)
            {
                ExtAspNet.Alert.Show("编码或者名称已经存在，请重新输入！", MessageBoxIcon.Information);
            }
            else
            {
                bool blRes = Manager.Save(SModel);
                if (blRes)
                {
                    ExtAspNet.Alert.Show("产品类别保存完成！", "提示！", "" + ExtAspNet.ActiveWindow.GetHidePostBackReference("refreshSort"));
                }
                else
                    ExtAspNet.Alert.Show("产品类别编辑失败！", MessageBoxIcon.Error);
            }
        } 
        #endregion

        #region Method
        private void InitControl()
        {
            bnClose.OnClientClick = ExtAspNet.ActiveWindow.GetHideReference();
            if (Request["Id"] != null)
            {
                Id = ValueHandler.GetIntNumberValue(Request["Id"]);
            }
        } 
        private void GetAndBind()
        {
            int ipid = 0;
            if (Id != 0)
            {
                SModel.PSort_ID = Id;
                SModel = Manager.GetModelById(SModel) as ProductSort;
                this.tb_PSort_Code.Text = SModel.PSort_Code;
                this.tb_PSort_Name.Text = SModel.PSort_Name;
                ipid = SModel.PSort_PSort_ID;
            }
            else
            //获取父类Id
            ipid= ValueHandler.GetIntNumberValue(Request["PSort_PSort_ID"]);
            if (ipid == 0)
            {
                this.lb_ParentName.Text = "公司产品库";
            }
            else
            {
                Sale_Model.ProductSort ps2 = new ProductSort();
                ps2.PSort_ID = ipid;
                ps2 = Manager.GetModelById(ps2) as ProductSort;
                this.lb_ParentName.Text = ps2.PSort_Name;
            }

        }
        #endregion

        #region Property
        ProductSortOp Manager = new ProductSortOp();
        new Sale_Model.ProductSort SModel = new Sale_Model.ProductSort();
        #endregion
    }
}
